package org.mikhaylov.cp.service.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;
import org.mikhaylov.cp.service.hibernate.HibernateUtil;

public class ServiceContextListener implements ServletContextListener {

	private static final Logger log = Logger.getLogger(ServiceContextListener.class);

	public void contextDestroyed(ServletContextEvent event) {
		log.info("Service context destruction started");
		
		log.info("Service context destruction complete");
	}

	public void contextInitialized(ServletContextEvent event) {
		log.info("Service context initialization started");
		
		initCommandContainer();
		initHibernate();

		log.info("Service context initialization complete");
	}

	/**
	 * Initializes CommandContainer.
	 * 
	 */
	private void initCommandContainer() {

		// initialize commands container
		// just load class to JVM
		try {
			Class.forName("org.mikhaylov.cp.service.command.ServiceCommandContainer");
		} catch (ClassNotFoundException ex) {
			throw new RuntimeException(ex);
		}
	}

	private void initHibernate() {
		try {
			HibernateUtil.class.getName();
			Class.forName(HibernateUtil.class.getName());
		} catch (ClassNotFoundException ex) {
			throw new RuntimeException(ex);
		}
	}
}